/**
* Copyright (c) 2004-2013 Luciano Xumerle. All rights reserved.
*
* This file is part of gephinfo.
*
* gephinfo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

package it.ciano.util;

/**
 * A class to manage informations about a single the command line's parameter
 * 
 * @author Luciano Xumerle
 * @version 0.0.1
 */
public class Parameter implements Comparable
{
    // GLOBAL PRIVATE VARS
    private int _position;
    private String _name = null;
    private String _value = null;
    private String _help = null;
    private boolean _isSelected;
    private boolean _withValue;
    private boolean _whiteline;

    /**
     * Class Constructor.
     *  
     * @param name  the parameter's name
     * @param value the parameter's value
     * @param help  help string for the parameter
     * @param withValue true if parameter has a value
     */
    public Parameter ( String name, String value, String help, boolean withValue )
    {
        _position = 0;
        _name = name;
        _value = value;
        _help = help;
        _withValue = withValue;
        _whiteline = false;
        _isSelected = false;
    }

    /**
     * Class Constructor.
     *  
     * @param position position into parameter help list
     * @param name  the parameter's name
     * @param value the parameter's value
     * @param help  help string for the parameter
     * @param withValue true if parameter has a value
     */
    public Parameter ( int position, String name, String value, String help, boolean withValue )
    {
        _position = position;
        _name = name;
        _value = value;
        _help = help;
        _withValue = withValue;
        _whiteline = false;
        _isSelected = false;
    }

    /**
      * Overriding toString method 
      * 
      * @return the stored par
      */
    public String toString()
    {
        return "name: " + _name + " - value: " + _value + "\nhelp: " + _help;
    }

    /**
     * The methos set a new value for the parameter.
     * 
     * @param value the new value
     */
    public void setValue( String value )
    {
        if ( _withValue )
            _value = value;
    }

    /**
     * The method gets the parameter value 
     * 
     * @return a String with the value
     */
    public String getValue()
    {
        return _value;
    }

    /**
     * The method sets the parameter position into help page
     * 
     * @param position the position
     */
    public void setPosition( int position )
    {
        _position = position;
    }

    /**
     * The method gets the parameter position into help page
     * 
     * @return integer with the position
     */
    public int getPosition()
    {
        return _position;
    }

    /**
     * The method gets the parameter help
     * 
     * @return a String with the help
     */
    public String getHelp()
    {
        return _help;
    }

    /**
     * The method set to true a valid parameter from the command line
     */
    public void setSelected()
    {
        _isSelected = true;
    }

    /**
     * The method gets true if parameter is used in the command line
     * 
     * @return treu or false
     */
    public boolean isSelected()
    {
        return _isSelected;
    }

    /**
     * Has the parameter a value?
     * 
     * @return true or false
     */
    public boolean withValue()
    {
        return _withValue;
    }

    /**
     * Add a whiteline after help to the parameter.
     */
    public void setWhiteLine()
    {
        _whiteline = true;
    }

    /**
     * Should the parameter have a whiteline after the help?
     * 
     * @return true or false
     */
    public boolean withWhiteLine()
    {
        return _whiteline;
    }


    /**
      * Overriding equals method 
      * 
      * @param o the object to compare
      * @return true or false
      */
    public boolean equals( Object o )
    {
        Parameter oth = ( Parameter ) o;
        return ( _name.equals( oth._name ) );
    }

    /**
      * Overriding clone method 
      * 
      * @return a new cloned object ID
      */
    public Object clone()
    {
        Parameter cloned = new Parameter( _position, _name, _value, _help, _withValue );
        if ( this.withWhiteLine() )
            cloned.setWhiteLine();
        return ( Object ) cloned;
    }

    /**
     * Overriding compareTo method
     * 
     * @param o the object to compare
     * @return an integer less or equal or major of 0
     */
    public int compareTo( Object o )
    {
        Parameter oth = ( Parameter ) o;
        if ( _position == oth._position )
            return ( this._name.compareTo( oth._name ) );
        else
            if ( _position > oth._position )
                return 1;
            else
                return -1;
    }

} // end class

